package com.octopus.media.fuwu;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Environment;
import android.os.IBinder;
import android.util.Log;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class VideoProvide extends Service {

    private List<String> videoList = new ArrayList<String>();
    private int countPlay = 0;

    private BroadcastReceiver myReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent == null) return;

            //顺序发送一个视频播放
            if (intent.getAction().equals("com.media.getBendiUrl")){
                Intent sendMsg = new Intent("com.media.sendBendiUrl");
                sendMsg.putExtra("URL",getUrl());
                sendBroadcast(sendMsg);
            }
            //同步本地视频到程序
            else if(intent.getAction().equals("com.media.listFile")){
                loocFile();
            }
            //清除程序上的视频，但硬盘中还存在
            else if(intent.getAction().equals("com.media.listClear")){
                listClear();
            }
            //清除硬盘上的视频
            else if(intent.getAction().equals("com.media.listDel")){
                listDel();
            }
        }
    };

    private void loocFile(){
        videoList.clear();
        countPlay = 0;
        File sdDir = Environment.getExternalStorageDirectory();
        File videos = new File(sdDir+"/YjrMedia/videos");
        if(videos!=null){
            File[] videosStr = videos.listFiles();
            if (videosStr == null) return;
            for(File video : videosStr){
                videoList.add(video.getAbsolutePath());
            }
        }
    }

    private void listClear(){
        videoList.clear();
    }

    private void listDel(){
        File sdDir = Environment.getExternalStorageDirectory();
        File videos = new File(sdDir+"/YjrMedia/videos");
        if(videos!=null){
            File[] videosStr = videos.listFiles();
            if (videosStr == null) return;
            for(File video : videosStr){
                video.delete();
            }
        }
    }

    private String getUrl(){
        String url = "";
        if (videoList.size()>0){
            url = videoList.get(countPlay%videoList.size());
        }
        countPlay++;

        return url;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        IntentFilter filter = new IntentFilter();
        filter.addAction("com.media.getBendiUrl");
        filter.addAction("com.media.listClear");
        filter.addAction("com.media.listFile");
        filter.addAction("com.media.listDel");
        registerReceiver(myReceiver, filter);

        Intent sendMsg = new Intent("com.media.listFile");
        sendBroadcast(sendMsg);
        Log.i("Kathy","onCreate - Thread ID = " + Thread.currentThread().getId());
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i("Kathy", "onStartCommand - startId = " + startId + ", Thread ID = " + Thread.currentThread().getId());
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        Log.i("Kathy", "onBind - Thread ID = " + Thread.currentThread().getId());
        return null;
    }

    @Override
    public void onDestroy() {
        unregisterReceiver(myReceiver);
        Log.i("Kathy", "onDestroy - Thread ID = " + Thread.currentThread().getId());
        super.onDestroy();
    }
}
